package jfb.examples.gmf.filesystem.diagram.part.transform;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.InputMap;

import jfb.examples.gmf.filesystem.diagram.part.FilesystemDiagramEditorPlugin;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.internal.util.BundleUtility;
import org.osgi.framework.Bundle;

import sun.util.locale.StringTokenIterator;

public class TransformationStorage {
	class TransformationPair {
		public Transformation mTransformation;
		public TransformationAction mAction;
	}
	
	public HashMap<String, TransformationPair> mTransformations;
	
	public TransformationStorage() {
		mTransformations = new HashMap<String, TransformationPair>();
	}
	
	public void addTransformation(Transformation transformation) {
		TransformationPair pair = new TransformationPair();
		pair.mTransformation = transformation;
		mTransformations.put(transformation.mDescription, pair);
	}
	
	public void initializeActions(IWorkbenchPart part) {
		for (TransformationPair pair : mTransformations.values()) {
			pair.mAction = new TransformationAction(part, pair.mTransformation);
			pair.mAction.init();
		}
	}
	
	public List<TransformationAction> getActionsForElement(EObject object) {
		FilesystemDiagramEditorPlugin.getInstance().logInfo("Class: " + object.eClass().getName());
		LinkedList<TransformationAction> actions = new LinkedList<TransformationAction>();
		for (TransformationPair pair : mTransformations.values()) {
			FilesystemDiagramEditorPlugin.getInstance().logInfo("Transformation class: " + pair.mTransformation.mObjectClass);
			if (object.eClass().getName().equals(pair.mTransformation.mObjectClass)) {
				actions.add(pair.mAction);
			}
		}
		return actions;
	}
	
	public void init(String name) {
		String data = "";
		InputStream input = getClass().getClassLoader().getResourceAsStream(name);
		try {
	        data = new java.util.Scanner(input).useDelimiter("\\A").next();
	    } catch (java.util.NoSuchElementException e) {
	    	return;
	    }
		FilesystemDiagramEditorPlugin.getInstance().logInfo("File data: " + data); 
		BufferedReader br = new BufferedReader(new StringReader(data));
		
		String next;
		try {
			while ((next = br.readLine()) != null)   {
				FilesystemDiagramEditorPlugin.getInstance().logInfo("Next token: " + next); 
				Transformation t = new Transformation();
				if (t.init(TransformationContextMenuProvider.TRANSFORMATION_FOLDER + next)) {
					addTransformation(t);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
